import java.io.InputStream;
import java.util.Vector;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class TileMap {
	public static int w = 100, h = 10;	

	public static byte[] map;// chua thong tin gia tri map

	public static int[] type;// chua thong tin loai type tai 1 vi tri map

	public static int size = 32;
	public int mapLenght = w * size ;

	public static Image img[];

	public String direc = "/map1";

	
	public boolean isOutmap = false;
	
//	public Cuop cuop;
	public  Coins coin, blood, box;
//	public Evils ninjaEvil, angel, flydragon, fish, boss;
	public static Vector vector = new Vector();	
	
	public TileMap()
	{
		mapLenght = w * size;
		Global.maplenght = mapLenght;
		System.out.println(Global.maplenght);
		loadtile("/tile.png");
		loadmap("/map01");	

	}
	
	public TileMap(int width, int height,  String pathTile, String pathMap)
	{
		w = width;
		h = height;
		mapLenght = w * size;
		Global.maplenght = mapLenght;
		loadtile(pathTile);
		loadmap(pathMap);
		
	}
	
	
	
	public void loadtile(String path) {
		try {
			Image imgTile = null;
			if (imgTile == null) {
				imgTile = Image.createImage(path);
				int nFrame = imgTile.getHeight() / size;
				img = new Image[nFrame];
				for (int i = 0; i < nFrame; i++) {
					img[i] = Image.createImage(imgTile, 0, i * size, size,
							size, 0);
				
				}
			}
		} catch (Exception e) {
			System.out.println("Error load tile");
			e.printStackTrace();
		}
	}
	
	public static int lech = 0;

	public void loadmap(String direc) {
		try {
			lech = Mscreen.heightScree - h *size;
			System.out.println("lech:" + lech);
			InputStream is = "".getClass().getResourceAsStream(direc);
			map = new byte[w * h];			
			for (int i = 0; i < w * h; i++) {
				map[i] = (byte) is.read();
				switch (map[i]) {
//				case 19:
//					ninjaEvil = new NinjaEvil((i%w)*size + size/2,(i/w)*size -size,Evils.NINJA);
//					Evils.evilVec.addElement(ninjaEvil);
//					break;
//				case 20:
//					boss = new EvilBoss((i%w)*size + size/2,(i/w)*size - size,Evils.BOSS);
//					Evils.evilVec.addElement(boss);
//					break;
//				case 25:
//					angel = new Angel((i%w)*size + size/2,(i/w)*size -size,Evils.ANGEL);
//					Evils.evilVec.addElement(angel);
//					break;
//				case 26:
//					fish = new Fish((i%w)*size + size/2,(i/w)*size -size,Evils.FISH);
//					Evils.evilVec.addElement(fish);
//					break;
//				case 27:
//					flydragon = new Flydragon((i%w)*size + size/2,(i/w)*size-size,Evils.FLYDRAGON);
//					Evils.evilVec.addElement(flydragon);
//					break;
				case 18:
					
						coin = new Coins(Coins.VCOIN,(i%w)*size + size/2,(i/w)*size);
						vector.addElement(coin);
					
					break;
				case 23:
					//if(!Level.isResetLevel)
					{
						blood = new Coins(Coins.BLOOD,(i%w)*size + size/2,(i/w)*size);
						vector.addElement(blood);
					}
					break;
				case 24:
					//if(!Level.isResetLevel)
					{
						box = new Coins(Coins.BOX,(i%w)*size + size/2,(i/w)*size);
						vector.addElement(box);
					}
					break;

	
				}
			}
		} catch (Exception e) {
			System.out.println("Error Load Map " + e.toString());
			e.printStackTrace();
			// TODO: handle exception
		}
	}
	
	public void drawMap(Graphics g)
	{
		for (int a = Cuop.sx; a < Cuop.sxe ; a++) {
			for (int b = Cuop.sy; b < Cuop.sye; b++) {
				int t = map[b * w + a] - 1;				
				if (t > -1) 
				{
					g.drawImage(img[t], a * size, b * size + lech -16, 0);
				}

			}
		}	
		

	}
	
	
	public static boolean testTileStand(int tile)
	{
		if(tile == 1 || tile ==2 || tile == 3 || tile == 4 || tile == 7 || tile == 8 || tile == 9 || tile == 10)
		{
			return true;
		}
		return false;
	}

	public static boolean testTileLeft(int tile)
	{
		if(tile == 1 || tile == 3 || tile == 4 || tile == 7 || tile == 9 || tile == 10 || tile ==12)
		{
			return true;
		}
		return false;
	}
	
	public static boolean testTileRight(int tile)
	{
		if(tile == 1 || tile == 3 || tile == 4 || tile == 7 || tile == 9 || tile == 10 || tile == 12)
		{
			return true;
		}
		return false;
	}
	
	public static boolean testTileGoal(int tile)
	{
		if(tile == 21)
		{
			return true;
		}
		return false;
	}

	
	public static boolean testTileBackRight(int tile)
	{
		if(tile == 3 || tile == 9)
		{
			return true;
		}
		return false;
	}
	
	public static boolean testTileBackLeft(int tile)
	{
		if(tile == 1 || tile == 7)
		{
			return true;
		}
		return false;
	}
	
	
	public static int getCurrentmapFoot(int yy, int xx)
	{	
		int i = map[((yy)/size)*w + (xx)/size];
		return i;		
	}
	
	
	public static int getCurrentmapHeadJump(int yh, int xx)
	{	
		int i = map[((yh)/size)*w + (xx)/size];
		return i;		
	}
	
	public static int getCurrentmapHead(int yy, int xx)
	{	
		int i = map[((yy)/size)*w + (xx)/size];
		return i;		
	}
	
	
	public static int getCurrentmapLeft(int yy, int xx)
	{	
		int i = map[((yy)/size)*w + (xx)/size];
		return i;		
	}
	
	public static int getCurrentmapRight(int yy, int xx)
	{	
		int i = map[((yy)/size)*w + (xx)/size];
		return i;		
	}
	
}
